package cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.button;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.common.ShowButtonOption;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockButtonType;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.business.CancelScheduleJobFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class AddCancelScheduleJobButtonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);
        boolean mayInterruptIfRunning = flowContext.getData("mayInterruptIfRunning", boolean.class, false);
        BlockButtonType blockButtonType = flowContext.getInstanceData(BlockButtonType.class, BlockButtonType.Primary);
        String buttonName = flowContext.getData("buttonName", String.class, "取消");
        String multipleButtonName = flowContext.getData("multipleButtonName", String.class, "批量取消");

        ListBlockOperationHandler blockOperationHandler = new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);

                flowMonitorOption.flowMonitorQuickFlow.startFlow(new CancelScheduleJobFlow())
                        .putTemporaryData("mayInterruptIfRunning", mayInterruptIfRunning)
                        .putInstanceData(listBlockRequest)
                        .execute();
                return null;
            }
        };
        List<ShowButtonOption> showButtonOptionList = new ArrayList<>();
        showButtonOptionList.add(new ShowButtonOption("running", true));
        showButtonOptionList.add(new ShowButtonOption("done", false));
        showButtonOptionList.add(new ShowButtonOption("cancel", false));
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation(buttonName, blockOperationHandler);
            blockOperation.blockButtonType = blockButtonType;
            blockOperation.showButtonOptionList.addAll(showButtonOptionList);
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
        {
            ListBlockHandlerBlockOperation blockOperation = new ListBlockHandlerBlockOperation(multipleButtonName, blockOperationHandler);
            blockOperation.blockButtonType = blockButtonType;
            blockOperation.showButtonOptionList.addAll(showButtonOptionList);
            blockOperation.position = BlockOperationPosition.MultipleSelect;
            entityTableListBlockOption.blockOperationList.add(blockOperation);
        }
    }

    @Override
    public String name() {
        return "添加取消定时任务按钮";
    }
}
